package com.core.util;

import java.io.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 对象序列化处理帮助类
 * 
 */
public class SerializableUtil {

	private static Log log = LogFactory.getLog(SerializableUtil.class);

	/**
	 * 将对象序列化到outputSteam中
	 * 
	 * @param outputStream
	 * @param object
	 */
	public static void writeObjectToOutputStream(OutputStream outputStream,
			Object object) {
		ObjectOutputStream objectOutputStream = null;
		try {
			objectOutputStream = new ObjectOutputStream(outputStream);
			objectOutputStream.writeObject(object);
			objectOutputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (Exception e) {
					objectOutputStream = null;
				}
			}
		}

	}

	/**
	 * 从流中读出对象
	 * 
	 * @param inputStream
	 * @param object
	 */
	public static void getObjectFromInputStream(InputStream inputStream,
			Object object) {
		ObjectInputStream objectInputStream = null;
		try {
			if (inputStream == null) {
				return;
			}
			objectInputStream = new ObjectInputStream(inputStream);
			object = (Object) objectInputStream.readObject();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (objectInputStream != null) {
					objectInputStream.close();
				}
			} catch (Exception e) {
				objectInputStream = null;
			}
		}

	}
}
